package com.tjetc.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tjetc.dao.DishMapper;
import com.tjetc.entity.Dish;
import com.tjetc.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    /***
     * 添加菜品信息
     * @param dish
     * @return
     */
    @Override
    public int insert(Dish dish) {
        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());
        return dishMapper.insert(dish);
    }

    /***
     * 根据id删除菜品信息
     * @param id
     * @return
     */
    @Override
    public int deleteById( Integer id) {
        return dishMapper.deleteById(id);
    }

    /***
     * 根据id查询菜品信息
     * @param id
     * @return
     */
    @Override
    public Dish selectById(Integer id) {
        return dishMapper.selectById(id);
    }


    @Override
    public PageInfo select2Page(String dishName, String merchantName, Integer upPrice, Integer downPrice, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Dish> users = dishMapper.selectByLikeUser(dishName, merchantName, upPrice, downPrice);
            return new PageInfo(users);
    }

    /***
     * 批量删除菜品
     * @param ids
     * @return
     */
    @Override
    public Integer deleteDishes(List<Integer> ids) {
        return dishMapper.deleteDishes(ids);
    }

    /***
     * 菜系id查询相关菜品
     * @param id
     * @param merchantId
     * @return
     */
    @Override
    public List<Dish> selectByCategoryId(int id,int merchantId) {
        return dishMapper.selectByCategoryId(id,merchantId);
    }

    /***
     * 查询所有菜品
     * @return
     */
    @Override
    public List<Dish> selectAll() {
        return dishMapper.selectAll();
    }

    /***
     * 根据用户id查询菜品
     * @param id
     * @return
     */
    @Override
    public List<Dish> selectByMerchantId(int id) {
        return dishMapper.selectByMerchantId(id);
    }


    @Override
    public PageInfo selectPageByMerchantId(int id,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Dish> list=dishMapper.selectByMerchantId(id);
        PageInfo pageInfo=new PageInfo(list);
        return pageInfo;
    }

    /***
     * 根据菜品名称查询菜品
     * @param name
     * @return
     */
    @Override
    public Dish selectByName(String name) {
        return dishMapper.selectByName(name);
    }

    /***
     * 更新菜品信息
     * @param dish
     * @return
     */
    @Override
    public Integer update(Dish dish) {
        return dishMapper.update(dish);
    }


    @Override
    public Integer passDishes(List<Integer> ids) {
        if(ids != null && ids.size() >= 1) return dishMapper.passDishes(ids);
        else throw new RuntimeException("输入的列表不能为空，请选择至少一个");
    }

    /***
     * 批量查询菜品信息
     * @param list
     * @return
     */
    @Override
    public List<Dish> selectByList(List<Integer> list) {
        return dishMapper.selectByList(list);
    }

    @Override
    public PageInfo selectDetailPageByMerchantId(int id, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Dish> list=dishMapper.selectDetailByMerchantId(id);
        return new PageInfo(list);
    }

    /***
     * 查询商家热销菜品
     * @param merchantId
     * @return
     */
    @Override
    public List<Dish> selectHot(int merchantId) {
        return dishMapper.selectHot(merchantId);
    }


    /**
     * 通过商家和类别获取菜品的列表（弃用）
     * @param id
     * @return
     */

    @Override
    public List<Dish> getMerchantExamine(Integer id) {
        return dishMapper.selectExamine(id);
    }
}
